package textfield;

import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;


import javax.microedition.midlet.MIDlet;
import java.util.Enumeration;
import java.io.InputStream;

import java.util.Vector;


/**
 * The text field demo displays all of the text field types on the screen
 * allowing the user to edit them at will.
 *
 * @version 2.0
 */
public class TextFieldDemo extends MIDlet implements ItemStateListener, CommandListener, Device {
    private Command lang1Command = new Command ("English", Command.OK, 2); //left 2
    private Command lang2Command = new Command ("Español", Command.OK, 3); //left 2
    private Command exitCommand = new Command ("Exit", Command.OK, 4); // left 2
    private Command backCommand = new Command("Back", Command.OK, 1); // right 7
    private Command chooseCommand = new Command("Choose", Command.STOP,1); // left 4
    private Command continueCommand = new Command("Continue", Command.STOP,1); //left 5
    private Command nextDrugCommand = new Command("Drugs","Next Drug", Command.SCREEN,5); //left 5
    private Command nextPatientCommand = new Command("Patient","Next Patient", Command.OK, 1); // left 5 
    Command lastCommand ;
    private boolean firstTime;
    private Form mainForm;
    private ChoiceGroup choiceGroup;
    private Question question;
    private Proto proto;
    private String lastAnswer = "";
    private int la = -1;
    Keys keys; 
    public TextFieldDemo () {
        firstTime = true;
    }

    protected void startApp () {
	parseXML();
	proto.initOps();
	if (firstTime) {
            mainForm = new Form ("Drug Questions");
	    //mainForm.addCommand (backCommand);
	    //	    keys = new Keys(mainForm);
            firstTime = false;
        }
	Display.getDisplay (this).setCurrent (mainForm);
	mainForm.setCommandListener (this);
	mainForm.setItemStateListener(this);
	// add commands for menu
	mainForm.addCommand(backCommand);
	mainForm.addCommand(lang1Command);
	mainForm.addCommand(exitCommand);
	mainForm.addCommand(lang2Command);
	proto.executeNext(this);

     }

    int nScreen = 1;
 
    
    public void writeNarrative(String narrative, boolean done) {
	mainForm.deleteAll();
	mainForm.append(new StringItem ("", narrative));	
	if (done) {
	    mainForm.addCommand(nextDrugCommand);
	    mainForm.addCommand(nextPatientCommand);
	}
	else {
	    addCommand(continueCommand);
	}
    }

    
    public Operation writeQuestion(Question question, int choice) {
	this.question = question;
	mainForm.deleteAll();
	mainForm.append (new StringItem ("", question.getQuestion()));
	choiceGroup = new ChoiceGroup("", Choice.EXCLUSIVE);
	mainForm.append (choiceGroup);
	for (int i = 1; i <= question.answers.list.size(); i++) {
	    Answer answer = (Answer) question.answers.getAnswers().elementAt(i-1);
	    choiceGroup.append(answer.getAnswer(), null);
	}
	addCommand(chooseCommand);
	if (choice >= 0) {
	    // skipping previously answered question
	    commandAction(chooseCommand, null);
	    return question.answers;
	}
	return question.answers;
    }

    public Operation skipOperation() {
	proto.executeNext(this);
	return proto.nextOp;
    }
    public void commandAction (Command c, Displayable s) {
       if (c == chooseCommand) {
	   mainForm.removeCommand(chooseCommand);
	    int choice = choiceGroup.getSelectedIndex();
	    question.answers.setChoice(new Integer(choice));
	    if (question.generalQuestion != null) {

		proto.patientAnswers.put(question.generalQuestion, 
					 new Integer(choice)); 
	    }
	    proto.executeNext(this); //process the answer
	    proto.executeNext(this); // now do the next op

      }
       else if (c == continueCommand) { 
	   mainForm.removeCommand(c);
	    proto.executeNext(this);

       }
       else if (c == nextDrugCommand || c == nextPatientCommand) {
	   mainForm.removeCommand(nextDrugCommand);
	   mainForm.removeCommand(nextPatientCommand);
	   // mainForm.removeCommand(exitCommand);
	   // mainForm.addCommand(backCommand);
	   if (c == nextPatientCommand) {
	       proto.reset();
	   }
	    proto.executeNext(this);

	
       }
       else if (c == backCommand) {
	   mainForm.removeCommand(lastCommand);
	   proto.backup(this);
       }
       else if (c == lang1Command) {
	   Reconstruct.lang = 1;
       }
       else if (c == lang2Command) {
	   Reconstruct.lang = 2;
       }

      else if (c == exitCommand) {
            destroyApp (false);
            notifyDestroyed ();
        }
  
    }
    public void itemStateChanged(Item item) {
	if (item instanceof ChoiceGroup) {
	    commandAction(chooseCommand, null);
	}
    }
    protected void destroyApp (boolean unconditional) {
    }

    protected void pauseApp () {
    }
    public Answer readAnswer(Enumeration answers) {
	return null;
    }
    public void done() {
    }
    	
    void writeDebug(String text) {
	mainForm.deleteAll();
	mainForm.append(new StringItem ("", text));	
	mainForm.addCommand(continueCommand);
    }
    public void parseXML() {
        try {
    	    InputStream is = getClass ().getResourceAsStream ("/small.xml");
	    proto = new Proto(is, this);
    	}
    	catch (Exception e) {
    	    e.printStackTrace();
    	}
    }
    String language = "English";
    public String getLanguage() {
	return language;
    }
    void addCommand(Command command) {
	mainForm.removeCommand(lastCommand);
	lastCommand = command;
	mainForm.addCommand(command);
    }
}